﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Estimate;

namespace EstimateService
{
    /// <summary>
    /// 考核报表数据接口
    /// </summary>
    public class HandlerZytHandinReport : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string year = context.Request.QueryString["year"] != null ? context.Request.QueryString["year"].ToString() : "";
            string month = context.Request.QueryString["month"] != null ? context.Request.QueryString["month"].ToString() : "";
            string semitype = context.Request.QueryString["semitype"] != null ? context.Request.QueryString["semitype"].ToString() : "";
            string type = context.Request.QueryString["type"] != null ? context.Request.QueryString["type"].ToString() : "";
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
            //json数据
            string json = ZytHandinReport(year, month, semitype, type, tid);

            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
            context.Response.Write(callback + "(" + json + ")");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 考核报表
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="semitype"></param>
        /// <param name="type">报表类型</param>
        /// <param name="tid"></param>
        /// <returns></returns>
        private string ZytHandinReport(string year, string month, string semitype, string type, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
            ForeSemiType semiType = (ForeSemiType)Enum.Parse(typeof(ForeSemiType), semitype);
            ZytHandinReportType reportType = (ZytHandinReportType)Enum.Parse(typeof(ZytHandinReportType), type);
            DateTime beginDate = new DateTime(int.Parse(year), int.Parse(month), 1);
            DateTime endDate = beginDate.AddMonths(1).AddDays(-1);

            Estimate.Reports.ZytHandinReport zytHandinReport = new Estimate.Reports.ZytHandinReport(connectionString, reportType, tid);
            zytHandinReport.PrepareData(beginDate, endDate, semiType);
            return zytHandinReport.GetReportDataString();
        }

    }
}